import axios from 'axios'
import { Toast } from 'vant';
axios.defaults.timeout = 60000;
axios.defaults.baseURL = 'http://62.234.47.40:8087/qjs-rest/'; //   新版本地址
// axios.defaults.baseURL = 'http://111.231.89.170:8087/qjs-rest/'; // 旧地址链接
//http request 拦截器
axios.interceptors.request.use(function(config) {
    return config;
}, function(error) {
    Message.error({
        message: '加载超时'
    });
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(
    response => {
        return response;
    },
    error => { //默认除了2XX之外的都是错误的，就会走这里
        if (error.response) {
            switch (error.response.code) {
                case 401:
            }
        }
        if (!error.response) {
            Toast('网络异常，请稍后再试...')
            return
        }
        return Promise.reject(error.response);
    }
);

export default {
    post(url, data, callback) {
        return axios.post(url, data, callback).then(function(response) {
            callback(response.data)
        }).catch((error) => {
            callback(error)
        })
    },

    get(url, data, callback) {
        return axios.get(url, data, callback).then(function(response) {
            callback(response.data)
        }).catch((error) => {
            callback(error)
        })
    },
    put(url, params, callback) {

        return axios.put(url, params, callback).then(function(response) {
            callback(response.data)
        }).catch((error) => {
            callback(error)
        })
    },
    delete(url, params, callback) {
        return axios.delete(url, params, callback).then(function(response) {
            callback(response.data);
        }).catch((error) => {
            callback(error)
        })
    },
    patch(url, params, callback) {
        return axios.patch(url, params, callback).then(function(response) {
            callback(response.data);
        }).catch((error) => {
            callback(error)
        })
    }

}